<?php
    
    class Userinfo_model extends CI_Model{
        public function __construct()
        {
            $this->load->database();
            //   $this->load->helper ( array (
            //   'form',
            //   'url'
            // ) );
            $this->load->library('session');
        }
        
        //  userId int(11) NOT NULL AUTO_INCREMENT,
        // name text,
        // password text,
        //   mobile0 varchar(20) ,
        //   mobile1 varchar(20) ,
        //   mobile2 varchar(20) ,
        //   mobile3 varchar(20) ,
        //   mobile4 varchar(20) ,
        //   level int ,
        
        public function login()
        {
            //echo "login:".$_POST ['username'];
            if (isset ( $_POST ['username'] )) {
                $password = md5(md5($_POST ['password']));
       
                $data = array (
                               'username' => $_POST ['username'],
                               'password' => $password
                               // 'password' => md5($_POST ['password'])
                               );
                
                $query = $this->db->get_where ( 'userinfo', array (
                                                                   'name' => $data ['username']
                                                                   ), 1, 0 );
                
                foreach ( $query->result () as $row ) {
                    $pass = $row->password;
                    $userId = $row->userId;
                    $lastTime = $row->lastTime;
                    $count = $row->count+1;
                }
                if ($pass == $data ['password']) {
                    
                    //for set session
                    $newdata = array(
                                     'userId'  => $userId ,
                                     'username'  =>  $data ['username'] ,
                                     'userip'     => $_SERVER['REMOTE_ADDR'],
                                     'level'     => $row->level,
                                     'count'     => $count,
                                     'lastTime'   =>$row->lastTime
                                     );
                    $this->session->set_userdata($newdata);
                    
                    //for update last tiem
                    $data = array('lastTime' => time(),
                                  'count' => $count
                                  );
                    
                    $this->db->where('userId', $userId);  
                    $this->db->update('userinfo', $data);

                    return true;
                }
            }
            // $sql = "SELECT * FROM userinfo WHERE 'name'='".$name."' and password='".$password."'";
            //$query = $this->db->query();
            
//            echo "false".$_POST ['submit'];
            //return $query->result_array();
            return false;
        }
        
        public function getOneUser($userId)
        {
          if (empty($userId)){
              $userId = $_SESSION['userId'];
            }
            $sql = "select* from userinfo where userId=".$userId;
        
            $query = $this->db->query($sql);
            $res  = $query->row();
        
            return $res;
        }
    
        public function getUsers()
        {
            $query = $this->db->get('userinfo');
            return $query->result_array();
        }
        
        public function getManagers()
        {
              $sql = "select* from userinfo where level=1 or level>=100";
              $query = $this->db->query($sql);
         
            return $query->result_array();
        }

        public function add()
        {
            //$this->load->helper('url');
            $password = md5(md5($this->input->post('password')));
            $data = array(
                          'name' => $this->input->post('name'),
                          'password' => $password,
                          'mobile' => $this->input->post('mobile'),
                          'level' => $this->input->post('level')
                          );
            
            $this->db->insert('userinfo', $data);
        }
        
        public function update()
        {
            $this->load->helper('url');
            $password = md5(md5($this->input->post('password')));
            $data = array(
                          'name' => $this->input->post('name'),
                          'password' => $password,
                          'mobile' => $this->input->post('mobile'),
                          'level' => $this->input->post('level')
                          );
            
            //print_r($data);
            $userId = $this->input->post('userId');
            //echo "userid:".$userId;
            
            
            $this->db->where('userId', $userId);  
            $this->db->update('userinfo', $data);
        }
        
        
        public function delete($userId)
        {
            $this->db->where('userId', $userId);  
            $this->db->delete('userinfo');
        }
        

        public function userExisted($userId)
        {
          if ($userId <= 0) {
            return false;
          }

          $userInfo = $this->getOneUser($userId);
          if ($userInfo) {
            return true;
          }

          return false;
        }
        
        // $this->db->where('字段名','字段值');  
        // $this->db->update('表名'，修改值的数组);
    }
